<template>
  <el-form>
    <el-form-item label="角色名称">
      <el-input v-model="role.name"></el-input>
    </el-form-item>
    <el-form-item label="角色编码">
      <el-input v-model="role.code"></el-input>
    </el-form-item>
    <el-form-item label="角色描述">
      <el-input type="textarea" v-model="role.description"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSubmit">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import Vue from 'vue'
import { createOrUpdate, getRoleById } from '@/services/role'
export default Vue.extend({
  name: 'RoleCreateOrEdit',
  components: {},
  props: {
    roleId: {
      type: [String, Number]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      role: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  created() {
    // 如果是编辑操作，则根据角色 ID 加载展示角色信息
    if (this.isEdit) {
      console.log('这是编辑角色')
      this.loadRole()
    }
  },
  methods: {
    async loadRole() {
      const { data } = await getRoleById(this.roleId)
      console.log(data)
      if (data.code === '000000') {
        debugger
        this.role = data.data
      }
    },
    async onSubmit() {
      const { data } = await createOrUpdate(this.role)
      console.log(data)
      if (data.code === '000000') {
        this.$message.success('操作成功')
        this.$emit('success')
      } else {
        this.$message.error(data.message)
      }
    },
    onCancel() {
      this.$emit('cancel')
    }
  }
})
</script>
<style lang="scss" scoped></style>
